//
//  LYMineOrderViewController.m
//  artist
//
//  Created by Jay_Apple on 16/4/14.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineOrderViewController.h"
#import "LYOrderCell.h"
#import "LYOrderDetailModel.h"
#import "LYOrderDetailViewController.h"
#import "LYPayFinshViewController.h"

@interface LYMineOrderViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, assign) NSInteger page;

@end

@implementation LYMineOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self.tableView.mj_header beginRefreshing];
    
    [[NSNotificationCenter defaultCenter] addObserver:self.tableView.mj_header selector:@selector(beginRefreshing) name:LYPaySuccessNotification object:nil];
}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self.tableView.mj_header];
}



#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    
    [LYRequestTool orderAddWithToken:LYToken type:LYOrderStateAll page:@"1" per_page:@"10" success:^(NSArray<LYOrderListModel *> *models) {
    
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;

        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_header endRefreshing];
        
    }];


}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
    [LYRequestTool orderAddWithToken:LYToken type:LYOrderStateAll page:page per_page:@"10" success:^(NSArray<LYOrderListModel *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        

        
        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_footer endRefreshing];
        
    }];

    
}


/**
 *  取消订单
 */
- (void)cancelOrderWithOrdersn:(NSString *)order_sn atIndexPath:(NSIndexPath *)indexPath{

    
    [LYRequestTool orderCancelWithToken:LYToken order_sn:order_sn success:^{
        
        [self.tableView.mj_header beginRefreshing];
        
    } failure:^(NSString *code, NSString *message) {
        
    }];


}

/**
 *  申请退款
 */
- (void)applyRefundWithOrdersn:(NSString *)order_sn{
    
    
    [LYRequestTool applyRefundWithToken:LYToken orderSn:order_sn success:^{
        
        [MBProgressHUD showSuccess:@"申请成功"];
        [self.tableView.mj_header beginRefreshing];
        
    } failure:^(NSString *code, NSString *message) {
        
        [MBProgressHUD showOnlyText:message];
        
    }];
    
}


#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}


#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
     LYOrderListModel *model = self.models[indexPath.row];
    
    if (model.status == orderStatusNoPay) {
        LYOrderDetailViewController *detailVC = [[LYOrderDetailViewController alloc] init];
        detailVC.order_sn = model.order_sn;
        [self.navigationController pushViewController:detailVC animated:YES];
    }else if(model.status != orderStatusCancel){
        
        LYPayFinshViewController *finishedVC = [[LYPayFinshViewController alloc] init];
        finishedVC.order_sn = model.order_sn;
        [self.navigationController pushViewController:finishedVC animated:YES];
    
    }


}

//真机下好像要实现这个方法才行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    LYOrderListModel *model = self.models[indexPath.row];
    if (model.status == orderStatusNoPay || model.status == orderStatusPay) {
        return YES;
    }
    
    return NO;
}


- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WS(weakSelf);
    
    LYOrderListModel *model = self.models[indexPath.row];
    UITableViewRowAction *action = nil;
    
    switch (model.status) {
        case orderStatusNoPay:{
            
            action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"取消订单" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                
                [weakSelf cancelOrderWithOrdersn:model.order_sn atIndexPath:indexPath];
                
            }];
        }
            break;
            
        case orderStatusPay:{
            action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"申请退款" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                
                [weakSelf applyRefundWithOrdersn:model.order_sn];
                
            }];
        }
            break;
            
        default:
            break;
    }
    
    
    action.backgroundColor = [UIColor redColor];
    
    return @[action];
    
}


#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYOrderCell";
    LYOrderCell *cell = [LYOrderCell cellWithTableView:tableView andIndetifier:cellID];
    
    //model
    LYOrderListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
    
}


@end
